using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UglyAnimals
{
    class Slingshot:Entity
    {
        // Position of the Player relative to the upper left side of the screen
        public override Vector2 Position
        {
            get;
            set;
        }

        // Get the width of the player      
        public override int Width
        {
            get{
                return frontTexture.Width;
            }
        }

        // Get the height of the player 
        public override int Height
        {
            get{
                return frontTexture.Height;
            }
        }

        public Texture2D frontTexture;
        public Texture2D backTexture;

        private Vector2 frontHandleSide;
        private Vector2 backHandleSide;
        public Vector2 animalPosition;
        private Vector2 animalStartPosition;

        public float MoveSpeed;

        public void Initialize(Vector2 position, Texture2D frontTexture, Texture2D backTexture, Vector2 animalStartPosition)
        {
            Position = position;
            this.frontTexture = frontTexture;
            this.backTexture = backTexture;
            frontHandleSide = new Vector2(Position.X + frontTexture.Width / 4, Position.Y + frontTexture.Height / 8);
            backHandleSide = new Vector2(Position.X + frontTexture.Width / 2, Position.Y + frontTexture.Height / 8);
            this.animalStartPosition = animalStartPosition;
            Active = true;
            MoveSpeed = 0;
        }

        public override void Update(GameTime gameTime){
            if (Active)
            {
                Position = new Vector2(Position.X - MoveSpeed, Position.Y);
                frontHandleSide = new Vector2(Position.X + frontTexture.Width / 2, Position.Y + frontTexture.Height / 4);
                backHandleSide = new Vector2(Position.X + frontTexture.Width, Position.Y + frontTexture.Height / 8);
                if (Position.X + frontTexture.Width <= 0)
                {
                    Active = false;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, bool inFrontofPlayer)
        {
            if (Active)
            {
                Texture2D blank = new Texture2D(graphicsDevice, 1, 1, false, SurfaceFormat.Color);
                blank.SetData(new[] { Color.White });
                float angle;
                float length;

                if (inFrontofPlayer)
                {
                    if ((int)animalPosition.X < (int)animalStartPosition.X)
                    {
                        angle = (float)Math.Atan2(frontHandleSide.Y - animalPosition.Y, frontHandleSide.X - animalPosition.X);
                        length = Vector2.Distance(animalPosition, frontHandleSide);

                        spriteBatch.Draw(blank, animalPosition, null, new Color(128, 0, 0),
                                   angle, Vector2.Zero, new Vector2(length, 7),
                                   SpriteEffects.None, 0);

                    }
                    spriteBatch.Draw(frontTexture, Position, null, Color.White, 0f, Vector2.Zero, 1.7f, SpriteEffects.None, 0f);
                }
                else
                {
                    spriteBatch.Draw(backTexture, Position, null, Color.White, 0f, Vector2.Zero, 1.7f, SpriteEffects.None, 0f);

                    if ((int)animalPosition.X < (int)animalStartPosition.X)
                    {
                        angle = (float)Math.Atan2(backHandleSide.Y - animalPosition.Y, backHandleSide.X - animalPosition.X);
                        length = Vector2.Distance(animalPosition, backHandleSide);

                        spriteBatch.Draw(blank, animalPosition, null, new Color(128, 0, 0),
                                   angle, Vector2.Zero, new Vector2(length, 7),
                                   SpriteEffects.None, 0);
                    }
                    else
                    {
                        angle = (float)Math.Atan2(backHandleSide.Y - frontHandleSide.Y, backHandleSide.X - frontHandleSide.X);
                        length = Vector2.Distance(frontHandleSide, backHandleSide);
                        spriteBatch.Draw(blank, frontHandleSide, null, new Color(128, 0, 0),
                                   angle, Vector2.Zero, new Vector2(length, 7),
                                   SpriteEffects.None, 0);
                    }
                }
            }
        }
    }
}
